<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:27:45
 * @LastEditTime: 2023-10-16 13:01:59
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\ExtendRelation\BelongsToTowKey;
use App\Observers\BaseObserver;
use App\Traits\CommonUserTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use DateTimeInterface;


use Brick\Math\BigDecimal;
use Brick\Math\Exception\MathException as BrickMathException;
use Brick\Math\RoundingMode;
use Illuminate\Support\Exceptions\MathException;

// use Illuminate\Database\Eloquent\Relations\BelongsTo;
// use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class BaseModel extends Model
    {
    use Cachable;
    use CommonUserTrait;
    public $_auto_resetlot_name = true;
    public $timestamps = FALSE;
    public static function boot()
        {
        parent::boot();
        self::observe(BaseObserver::class);
        // Setup event bindings...
        }

    protected function newBelongsToTowKey(Builder $query, Model $child, $second_foreignKey, $second_ownerKey, $foreignKey, $ownerKey, $relation)
        {
        return new BelongsToTowKey($query, $child, $second_foreignKey, $second_ownerKey, $foreignKey, $ownerKey, $relation);
        }

    public function belongsToTowKey($related, $second_foreignKey = null, $second_ownerKey = null, $foreignKey = null, $ownerKey = null, $relation = null)
        {
        // If no relation name was given, we will use this debug backtrace to extract
        // the calling method's name and use that as the relationship name as most
        // of the time this will be what we desire to use for the relationships.
        if (is_null($relation)) {
            $relation = $this->guessBelongsToRelation();
            }

        $instance = $this->newRelatedInstance($related);

        // If no foreign key was supplied, we can use a backtrace to guess the proper
        // foreign key name by using the name of the relationship function, which
        // when combined with an "_id" should conventionally match the columns.
        if (is_null($foreignKey)) {
            $foreignKey = Str::snake($relation) . '_' . $instance->getKeyName();
            }

        // Once we have the foreign key names we'll just create a new Eloquent query
        // for the related models and return the relationship instance which will
        // actually be responsible for retrieving and hydrating every relation.
        $ownerKey = $ownerKey ?: $instance->getKeyName();
        $query    = $instance->newQuery();

        return $this->newBelongsToTowKey(
            $query,
            $this,
            $second_foreignKey,
            $second_ownerKey,
            $foreignKey,
            $ownerKey,
            $relation
        );
        }
    protected function asDecimal($value, $decimals)
        {
        try {
            return BigDecimal::of($value)->toScale($decimals, RoundingMode::HALF_UP)->toFloat();
            }
        catch ( BrickMathException $e ) {
            throw new MathException('Unable to cast value to a decimal.', previous: $e);
            }
        }

    /**
     * Prepare a date for array / JSON serialization.
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
        {
        return $date->format('Y-m-d H:i:s');
        }
    }
